package com.icode.core.dto.order;

import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.dto.house.HouseWeekDTO;
import com.icode.core.dto.user.CityDTO;
import com.icode.core.dto.user.DistrictDTO;
import com.icode.core.dto.user.ProvinceDTO;
import com.icode.core.model.house.HouseInfo;
import com.icode.core.model.house.HouseWeek;
import com.icode.core.model.order.AlipayStatus;
import com.icode.core.model.order.Order;
import com.icode.core.model.order.OrderInfo;
import com.icode.core.model.user.UserInfo;
import org.hibernate.validator.constraints.NotEmpty;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-20
 * Time: 下午2:23
 * To change this template use File | Settings | File Templates.
 */
public class OrderDTO {
    private String id;
    private String guid;
    private HouseInfoDTO houseInfoDTO;
    private HouseWeekDTO houseWeekDTO;
    private OrderInfoDTO orderInfoDTO;
    @NotEmpty(message = "不能有非法字符!")
    private String realName;
    @NotEmpty(message = "请填写身份证!")
    private String IDCard;
    private String phoneNum;
    private String province;
    private String city;
    private String district;
    private String area;
    private List<ProvinceDTO> provinces;
    private List<CityDTO> cities;
    private List<DistrictDTO> districts;
    private String houseWeekGuid;


    public OrderDTO() {
    }

    public OrderDTO(String id, String guid, HouseInfoDTO houseInfoDTO, HouseWeekDTO houseWeekDTO, OrderInfoDTO orderInfoDTO) {
        this.id = id;
        this.guid = guid;
        this.houseInfoDTO = houseInfoDTO;
        this.houseWeekDTO = houseWeekDTO;
        this.orderInfoDTO = orderInfoDTO;
    }

    public OrderDTO(HouseInfoDTO houseInfoDTO, HouseWeekDTO houseWeekDTO, OrderInfoDTO orderInfoDTO, String realName, String IDCard, String phoneNum, String province, String city, String district, String area) {
        this.houseInfoDTO = houseInfoDTO;
        this.houseWeekDTO = houseWeekDTO;
        this.orderInfoDTO = orderInfoDTO;
        this.realName = realName;
        this.IDCard = IDCard;
        this.phoneNum = phoneNum;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
    }

    public OrderDTO(String id, String guid, HouseInfoDTO houseInfoDTO, HouseWeekDTO houseWeekDTO, OrderInfoDTO orderInfoDTO, String realName, String IDCard, String phoneNum, String province, String city, String district, String area) {
        this.id = id;
        this.guid = guid;
        this.houseInfoDTO = houseInfoDTO;
        this.houseWeekDTO = houseWeekDTO;
        this.orderInfoDTO = orderInfoDTO;
        this.realName = realName;
        this.IDCard = IDCard;
        this.phoneNum = phoneNum;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
    }

    public static Order toOrder(OrderDTO orderDTO, HouseWeek houseWeek, HouseInfo houseInfo, UserInfo usersInfo, String orderTime) {
        BigDecimal price = new BigDecimal(houseWeek.price());
        BigDecimal buyNum = new BigDecimal(orderDTO.orderInfoDTO.getNum());
        BigDecimal multiplyResult = price.multiply(buyNum);
        OrderInfo orderInfo = new OrderInfo(Integer.parseInt(orderDTO.orderInfoDTO.getNum()), String.valueOf(multiplyResult), AlipayStatus.PAY_FAIL, orderTime);
        Order order = new Order(houseWeek, houseInfo, orderInfo, usersInfo);
        return order;
    }

    public void initAddress(List<ProvinceDTO> provinces, List<CityDTO> cities, List<DistrictDTO> districts) {
        this.provinces = provinces;
        this.cities = cities;
        this.districts = districts;
    }

    public static List<OrderDTO> toDTOs(List<Order> orders) {
        List<OrderDTO> orderDTOs = new ArrayList<>();
        for (Order o : orders) {
            OrderDTO orderDTO = toDTO(o);
            orderDTOs.add(orderDTO);
        }
        return orderDTOs;
    }

    public static OrderDTO toDTO(Order order) {
        HouseInfoDTO houseInfoDTO = HouseInfoDTO.toDTO(order.houseInfo());
        HouseWeekDTO houseWeekDTO = HouseWeekDTO.toDTO(order.houseWeek());
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO(order.houseWeek().price(), String.valueOf(order.orderInfo().num()), order.orderInfo().totalPrice(), order.orderInfo().status().getLabel(), order.orderInfo().time(), order.orderInfo().alipayTradeNo());
        OrderDTO orderDTO = new OrderDTO(order.id(), order.guid(), houseInfoDTO, houseWeekDTO, orderInfoDTO, order.userInfo().realName(), order.userInfo().IDCard(), order.userInfo().user().getUsername(), String.valueOf(order.userInfo().province().id()), String.valueOf(order.userInfo().city().id()), String.valueOf(order.userInfo().district().id()), order.userInfo().area());
        return orderDTO;
    }

    public void initProvinces(List<ProvinceDTO> provinces) {
        this.provinces = provinces;
    }

    public String getHouseWeekGuid() {
        return houseWeekGuid;
    }

    public void setHouseWeekGuid(String houseWeekGuid) {
        this.houseWeekGuid = houseWeekGuid;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getGuid() {

        return guid;
    }

    public String getId() {

        return id;
    }

    public HouseInfoDTO getHouseInfoDTO() {
        return houseInfoDTO;
    }

    public void setHouseInfoDTO(HouseInfoDTO houseInfoDTO) {
        this.houseInfoDTO = houseInfoDTO;
    }

    public void setOrderInfoDTO(OrderInfoDTO orderInfoDTO) {
        this.orderInfoDTO = orderInfoDTO;
    }

    public OrderInfoDTO getOrderInfoDTO() {
        return orderInfoDTO;
    }

    public String getRealName() {
        return realName;
    }

    public String getIDCard() {
        return IDCard;
    }

    public HouseWeekDTO getHouseWeekDTO() {
        return houseWeekDTO;
    }

    public void setHouseWeekDTO(HouseWeekDTO houseWeekDTO) {
        this.houseWeekDTO = houseWeekDTO;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public List<ProvinceDTO> getProvinces() {
        return provinces;
    }

    public List<CityDTO> getCities() {
        return cities;
    }

    public List<DistrictDTO> getDistricts() {
        return districts;
    }

    public String getDistrict() {
        return district;
    }

    public void setProvinces(List<ProvinceDTO> provinces) {
        this.provinces = provinces;
    }

    public void setCities(List<CityDTO> cities) {
        this.cities = cities;
    }

    public void setDistricts(List<DistrictDTO> districts) {
        this.districts = districts;
    }

    public String getArea() {
        return area;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public void setIDCard(String IDCard) {
        this.IDCard = IDCard;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setArea(String area) {
        this.area = area;
    }

}
